// pages/mes/open-live/open-live.js
const app = getApp();
import Toast from '../../../component/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        liveUrl: "",
        isLiving:false
    },
    inputLiveUrl(e) {
        this.setData({
            liveUrl: e.detail.value
        });
    },
    openLive() {
        if (this.checkUrl(this.data.liveUrl)){
            this.saveUserLiveUrl();
        }else{
            Toast("请输入正确的URL地址");
        }
    },
    finishLive() {
        let that = this;
        if(!this.data.isLiving){
            return;
        }
        wx.showModal({
            title: '提示',
            content: '确定要结束当前直播吗？',
            cancelColor: '#E71015',
            success: function(res) {
                if(res.confirm){
                    that.deleteLiveUrl();
                }
            }
        })
    },
    checkUrl(urlString) {
        if (urlString != "") {
            var reg = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;
            if (!reg.test(urlString)) {
                return false;
            }
            return true;
        }
        return false;
    },
    saveUserLiveUrl(){
        let that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.saveUserLiveUrl,
            data: {
                UserInfoId: app.userInfo.Id,
                LiveUrl: that.data.liveUrl
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function(res) {
                Toast(res.data.Message);
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    setTimeout(function () {
                        wx.navigateBack({
                            delta: 1,
                        })
                    }, 1000)
                }
            },
            fail: function(res) {console.log(res);},
            complete: function(res) {wx.hideLoading();},
        });
    },
    deleteLiveUrl(){
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.deleteUserLiveUrl,
            data: {
                UserInfoId: app.userInfo.Id
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function (res) {
                Toast(res.data.Message);
                if(res.statusCode == 200 && res.data.CodeStatus == 20000){
                    setTimeout(function(){
                        wx.navigateBack({
                            delta: 1,
                        })
                    },1000)
                }
            },
            fail: function (res) { console.log(res); },
            complete: function (res) { wx.hideLoading(); },
        });
    },
    getAllInfo() {
        let that = this;
        wx.request({
            url: app.globalData.serverAddress.getAllInfo,
            data: {
                userId: app.userInfo.Id
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'GET',
            success: function (res) {
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    let BodyContent = res.data.BodyContent;
                    if (BodyContent.LiveInfo) {
                        that.setData({
                            liveUrl: BodyContent.LiveInfo.LiveUrl,
                            isLiving:true
                        });
                    }
                    
                } else {
                    console.log("success：" + JSON.stringify(res));
                }
            },
            fail: function (res) {
                console.log("fail" + JSON.stringify(res));
            }
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getAllInfo();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})